package com.siao.core
{
	import flash.external.ExternalInterface;
	import flash.net.LocalConnection;
	import flash.system.Capabilities;
	import flash.system.TouchscreenType;
	
	/**
	 * 播放器核心
	 * @author SiaoLeon
	 */
	public class FlashPlayer
	{
		/**
		 * 获取当前播放器版本
		 * @return
		 */
		public static function get version():String
		{
			var version:String = flash.system.Capabilities.version;
			return version.slice(version.indexOf(" ") + 1).split(",").join(".");
		}
		
		/**
		 * 获取当前浏览器类型
		 * @return
		 */
		public static function get currentBrowser():int
		{
			if (!ExternalInterface.available)
			{
				return WebBrowser.NONE;
			}
			
			var browserAgent:String = ExternalInterface.call("function getBrowser(){return navigator.userAgent;}");
			
			if (browserAgent != null && browserAgent.indexOf("MSIE") >= 0)
			{
				return WebBrowser.IE;
			}
			
			if (browserAgent != null && browserAgent.indexOf("Chrome") >= 0)
			{
				return WebBrowser.Chrome;
			}
			
			if (browserAgent != null && browserAgent.indexOf("Firefox") >= 0)
			{
				return WebBrowser.Firefox;
			}
			
			if (browserAgent != null && browserAgent.indexOf("Safari") >= 0)
			{
				return WebBrowser.Safari;
			}
			
			if (browserAgent != null && browserAgent.indexOf("Opera") >= 0)
			{
				return WebBrowser.Opera;
			}
			
			return WebBrowser.UNKNOW;
		}
		
		/**
		 * 获取当前运行环境
		 * @return
		 */
		public static function get currentRuntime():uint
		{
			if (ExternalInterface.available)
			{
				return Runtime.WEB;
			}
			
			if (Capabilities.touchscreenType != TouchscreenType.NONE)
			{
				return Runtime.MOBILE;
			}
			
			return Runtime.DESKTOP;
		}
		
		/**
		 * 检测当前是否为调试编译
		 */
		public static function get isDebug():Boolean
		{
			try
			{
				return new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;
			}
			catch (err:Error)
			{
				return false;
			}
			return false;
		}
		
		/**
		 * 强制执行垃圾回收机制
		 */
		public static function GC():void
		{
			try
			{
				(new LocalConnection).connect("foo");
				(new LocalConnection).connect("foo");
			}
			catch (err:Error)
			{
			}
		}
		
		/**
		 * 注册播放器鼠标滚轮事件回调
		 * @param callback 回调方法
		 */
		public static function RegisterMouseWheelHandler(callback:Function):void
		{
			if (MouseWheelCallback == null)
			{
				MouseWheelCallback = new Array();
			}
			
			if (ExternalInterface.available)
			{
				try
				{
					ExternalInterface.call("FlashMouseController.mouseWheel", false);
					ExternalInterface.addCallback("onFlashMouseWheel", onFlashMouseWheel);
				}
				catch (err:Error)
				{
				}
			}
			
			MouseWheelCallback.push(callback);
		}
		
		/**
		 * 移除播放器鼠标滚轮事件回调
		 * @param callback 回调方法
		 */
		public static function RemoveMouseWheelHandler(callback:Function):void
		{
			if (MouseWheelCallback == null)
			{
				return;
			}
			
			for (var i:int = MouseWheelCallback.length - 1; i >= 0; i--)
			{
				if (callback == MouseWheelCallback[i])
				{
					MouseWheelCallback.splice(i, 1);
				}
			}
			
			if (MouseWheelCallback.length <= 0)
			{
				ClearMouseWheelHandler();
			}
		}
		
		/**
		 * 清除播放器所有鼠标滚轮事件回调
		 */
		public static function ClearMouseWheelHandler():void
		{
			MouseWheelCallback = [];
			ExternalInterface.call("FlashMouseController.mouseWheel", true);
		}
		
		private static var MouseWheelCallback:Array;
		
		private static function onFlashMouseWheel(delta:int):void
		{
			for each (var callback:Function in MouseWheelCallback.concat())
			{
				callback.call(null, delta);
			}
		}
		
		/**
		 * 注册播放器鼠标右键事件回调
		 * @param callback 回调方法
		 */
		public static function RegisterRightClickHandler(callback:Function):void
		{
			if (RightClickCallback == null)
			{
				RightClickCallback = new Array();
			}
			
			if (ExternalInterface.available)
			{
				try
				{
					ExternalInterface.call("FlashMouseController.init");
					ExternalInterface.addCallback("onFlashRightClick", onFlashRightClick);
				}
				catch (err:Error)
				{
				}
			}
			
			RightClickCallback.push(callback);
		}
		
		/**
		 * 移除播放器鼠标右键事件回调
		 * @param callback 回调方法
		 */
		public static function RemoveRightClickHandler(callback:Function):void
		{
			if (RightClickCallback == null)
			{
				return;
			}
			
			for (var i:int = RightClickCallback.length - 1; i >= 0; i--)
			{
				if (callback == RightClickCallback[i])
				{
					RightClickCallback.splice(i, 1);
				}
			}
			
			if (RightClickCallback.length <= 0)
			{
				ClearRightClickHandler();
			}
		}
		
		/**
		 * 清除播放器所有鼠标右键事件回调
		 */
		public static function ClearRightClickHandler():void
		{
			RightClickCallback = [];
		}
		
		private static var RightClickCallback:Array;
		
		private static function onFlashRightClick():void
		{
			for each (var callback:Function in RightClickCallback.concat())
			{
				callback.call();
			}
		}
	
	}
}
